package com.showcai.controller.media;

import com.github.pagehelper.PageInfo;
import com.showcai.controller.base.BaseController;
import com.showcai.module.Media;
import com.showcai.module.MediaClassify;
import com.showcai.result.ResultObj;
import com.showcai.result.ReturnCode;
import com.showcai.service.MediaClassifyService;
import com.showcai.service.MediaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Baggio on 2017-11-15.
 */
@RequestMapping("/media")
@Controller
public class MediaController extends BaseController {
    @Autowired
    MediaService mediaService;
    @Autowired
    MediaClassifyService mediaClassifyService;

    /**
     * 有声读物列表页面
     *
     * @return
     */
    @RequestMapping("/mediaList")
    public ModelAndView getMediaList(Byte[] classify) {
        ModelAndView m = new ModelAndView("/media/mediaList");
        Map<String, String> map = super.showRequestParameters();
        if(classify!=null && classify.length>0){
            map.put("classify",String.valueOf(classify[classify.length-1]));
        }
        PageInfo<Media> pageInfo = mediaService.getMediaList(map);
        m.addObject("pageInfo", pageInfo);
        m.addObject("map", map);
        if(classify!=null && classify.length>0){
            m.addObject("classify", classify[classify.length-1]);
        }

        return m;
    }


    /**
     * 编辑有声读物页面
     * @param tid
     * @return
     */
    @RequestMapping(value = "/media")
    public ModelAndView media(Long tid) {
        ModelAndView m = new ModelAndView("/media/media");
        if (tid != null) {
            Map<String, String> map = new HashMap<>();
            Media media = mediaService.getMediaById(tid);
            m.addObject("media", media);
            List<MediaClassify> allMediaClassifylist = mediaClassifyService.getMediaClassifyTree(7L);
            m.addObject("allMediaClassifyList", allMediaClassifylist);
        }
        List<MediaClassify> mediaClassifyList = mediaClassifyService.getMediaClassify(new HashMap<>());
        m.addObject("mediaClassifyList", mediaClassifyList);
        return m;
    }

    /**
     * 更新有声读物
     *
     * @param media
     * @return
     */
    @RequestMapping(value = "/modifyMedia")
    @ResponseBody
    public String modifyMedia(Byte[] classify, Media media, @RequestParam(value = "picUrl", required = false) MultipartFile picUrl, @RequestParam(value = "file", required = false) MultipartFile file) {
        ResultObj resultObj = new ResultObj();
        String mediaPicUrl = "";
        String mediaUrl = "";
        String filePath = "showcai/media";
        if(classify.length>0){
            media.setClassify(classify[classify.length-1]);
        }
        try {
            if (picUrl != null) {
                try {

                    mediaPicUrl = upLoadFile(picUrl.getInputStream(), filePath);
                    logger.info("resourceUrl=============>>>" + mediaPicUrl);
                    media.setPic(mediaPicUrl);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (file != null) {
                try {
                    mediaUrl = super.putObject(file.getInputStream(), filePath, file.getOriginalFilename());
                    logger.info("resourceUrl=============>>>" + mediaUrl);
                    media.setUrl(mediaUrl);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            mediaService.modifyMedia(media);
            resultObj.setMsg("操作成功！");
        } catch (Exception e) {
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return resultObj.toJson();
    }

    /**
     * 更新有声读物状态
     * @param media
     * @return
     */
    @RequestMapping(value = "/modifyMediaStatus")
    @ResponseBody
    public String modifyMediaStatus(Media media) {
        ResultObj resultObj = new ResultObj();
        try {
            mediaService.modifyMedia(media);
            resultObj.setMsg("操作成功！");
        } catch (Exception e) {
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return resultObj.toJson();
    }




}
